package cn.springcloud.alibaba.core.base;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Result<T> extends BaseResponse {

	private T data;

	public Result() {

	}

	public Result(ResultCode resultCode) {
		super(resultCode.getCode(), resultCode.getMessage());
	}

	public Result(ResultCode resultCode, T data) {
		super(resultCode.getCode(), resultCode.getMessage());
		this.data = data;
	}

	public Result(T data, Integer code, String message) {
		super(code, message);
		this.data = data;
	}

	public Result<T> setResultCode(ResultCode resultCode) {
		super.setResultCode(resultCode);
		return this;
	}

	public static <T> Result<T> of(T data, Integer code, String msg) {
		return new Result<>(data, code, msg);
	}

	public static <T> Result<T> success(T model, String msg) {
		return of(model, 0, msg);
	}

	public static <T> Result<T> success(T model) {
		return of(model, 0, "请求成功");
	}

	public static <T> Result<T> error(ResultCode resultCode) {
		return of(null, resultCode.getCode(), resultCode.getMessage());
	}

	public static <T> Result<T> failed(String msg) {
		return of(null, -1, msg);
	}

	public static <T> Result<T> failed(T model, String msg) {
		return of(model, -1, msg);
	}

	public static Boolean checkSuccess(Result<?> result) {
		if (result == null) return false;
		return result.getCode() == 0;
	}

	public static Boolean checkUnSuccess(Result<?> result) {
		return !checkSuccess(result);
	}
}
